/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.game;

/**
 *
 * @author daz
 */
public class GameRoom {

    public GameTable[] listTable;
    public GamePlayer[] listPlayer;
    private GameManager gameManager;
    private int numberPlayersInRoom = 0;
    private int gameType;
    private long moneyBetDefault;

    public GameRoom(int gameType, GameManager gameManager, long moneyBetDefault) {
        this.gameType = gameType;
        this.gameManager = gameManager;
        this.moneyBetDefault = moneyBetDefault;
        listTable = new GameTable[GameManager.NUMBER_TABLES_IN_ROOM];
        listPlayer = new GamePlayer[GameManager.NUMBER_TABLES_IN_ROOM * GameTable.MAX_USERS];
        initTable();
    }

    private void initTable() {
        switch (gameType) {
            case GameManager.PHOM:
                for (int i = 0; i < GameManager.NUMBER_TABLES_IN_ROOM; i++) {
                    listTable[i] = new PhomTable(gameManager, this, moneyBetDefault);
                }
                break;

            case GameManager.CHESS:
                for (int i = 0; i < GameManager.NUMBER_TABLES_IN_ROOM; i++) {
                    listTable[i] = new ChessTable(gameManager, this, moneyBetDefault);
                }
                break;

            case GameManager.TIEN_LEN:
                for (int i = 0; i < GameManager.NUMBER_TABLES_IN_ROOM; i++) {
                    listTable[i] = new TienlenTable(gameManager, this, moneyBetDefault);
                }
                break;
            case GameManager.XENG:
                break;

            case GameManager.BA_CAY:
                for (int i = 0; i < GameManager.NUMBER_TABLES_IN_ROOM; i++) {
                    listTable[i] = new BacayTable(gameManager, this, moneyBetDefault);
                }
                break;
        }
    }

    public synchronized boolean addPlayer(GamePlayer player) {
        if (numberPlayersInRoom < (GameManager.NUMBER_TABLES_IN_ROOM * GameTable.MAX_USERS)) {
            listPlayer[numberPlayersInRoom] = player;
            numberPlayersInRoom++;
            return true;
        }
        return false;
    }

    public synchronized void removePlayer(long userId) {
        for (int i = 0; i < numberPlayersInRoom; i++) {
            if (listPlayer[i] == null) {
                continue;
            }
            if (userId == listPlayer[i].getUserId()) {
                for (int j = i; j < numberPlayersInRoom - 1; j++) {
                    listPlayer[j] = listPlayer[j + 1];
                }
                listPlayer[numberPlayersInRoom - 1] = null;
                numberPlayersInRoom--;
                return;
            }
        }
    }

    public synchronized void removePlayer(String name) {
        for (int i = 0; i < numberPlayersInRoom; i++) {
            if (listPlayer[i] == null) {
                continue;
            }
            if (listPlayer[i].getUserName().equals(name)) {
                for (int j = i; j < numberPlayersInRoom - 1; j++) {
                    listPlayer[j] = listPlayer[j + 1];
                }
                listPlayer[numberPlayersInRoom - 1] = null;
                numberPlayersInRoom--;
                return;
            }
        }
    }

    public int getNumberOfPlayersInRoom() {
        return numberPlayersInRoom;
    }

    public void sendToAllPlayerInRoom(String msg) {
        for (int i = 0; i < listPlayer.length; i++) {
            if (listPlayer[i] != null) {
                listPlayer[i].response(msg);
            }
        }
    }

    public void sendToAllPlayerInGame(String msg, int gameType) {
        gameManager.sendToAllPlayerInGame(msg, gameType);
    }
}
